package com.tadpole.music.view.activity.login;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

import com.tadpole.music.R;
import com.tadpole.music.bean.PushBean;
import com.tadpole.music.config.Constant;
import com.tadpole.music.config.UrlConfig;
import com.tadpole.music.iView.login.RegisterIView;
import com.tadpole.music.iView.login.SendCodeIView;
import com.tadpole.music.presenter.login.RegisterPresenter;
import com.tadpole.music.presenter.login.SendCodePresenter;
import com.tadpole.music.utils.Mutils;
import com.tadpole.music.utils.NoDoubleClickListener;
import com.tadpole.music.utils.SpUtil;
import com.tadpole.music.utils.StatusBarUtil;
import com.tadpole.music.utils.ToastUtils;
import com.tadpole.music.view.activity.base.BaseActivity;
import com.tadpole.music.view.activity.home.NewInfoListActivity;
import com.tadpole.music.view.activity.question.SimulationExerciseActivity;
import com.tadpole.music.view.activity.web.H5Activity;

/**
 * 注册
 * Created By 孙岩
 * on 2020/9/7
 */
public class LoginRegisterActivity extends BaseActivity implements SendCodeIView, RegisterIView {
    //回退
    private View view_back_icon;
    //手机号
    private EditText etPhone;
    //验证码
    private EditText etCode;
    //发送验证码
    private TextView tvSendCode;
    //选择
    private LinearLayout llSelect;
    //密码
    private EditText etPassword;
    //再次密码
    private  EditText etRPassword;
    //选择
    private ImageView ivSelect;
    //一键登录注册
    private TextView tvLogin;
    //用户协议
    private TextView tv_notice;
    //判断是否选中已阅读的标识
    private int flag = 0;
    //老师还是学生的标识
    private int role;
    //倒计时
    private TimeCount time;
    //发送验证码
    private SendCodePresenter sendCodePresenter;
    //是否发送了验证码
    private boolean isSendCode = false;
    //注册
    private RegisterPresenter registerPresenter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_register);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        time = new TimeCount(60000,1000);
        role = getIntent().getIntExtra("role",0);
        //将头部状态栏置为透明
        StatusBarUtil.setTranslucentForImageViewInFragment(this, 0, null);
        initViews();
        initListeners();

        sendCodePresenter = new SendCodePresenter();
        sendCodePresenter.attachView(this);

        registerPresenter = new RegisterPresenter();
        registerPresenter.attachView(this);
    }

    //初始化数据
    private void initViews(){
        view_back_icon = findViewById(R.id.view_back_icon);
        etPhone = findViewById(R.id.etPhone);
        etCode = findViewById(R.id.etCode);
        tvSendCode = findViewById(R.id.tvSendCode);
        llSelect = findViewById(R.id.llSelect);
        ivSelect = findViewById(R.id.ivSelect);
        tvLogin = findViewById(R.id.tvLogin);
        tv_notice = findViewById(R.id.tv_notice);
        etPassword = findViewById(R.id.etPassword);
        etRPassword = findViewById(R.id.etRPassword);

        SpannableString spannable = new SpannableString ("已阅读并同意蝌蚪音基《隐私政策》 ");

        //这个一定要记得设置，不然点击不生效
        tv_notice.setMovementMethod(LinkMovementMethod.getInstance());
        tv_notice.setHighlightColor(Color.TRANSPARENT);
        //用户协议
        spannable.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                if(Mutils.isNetworkAvailable()){
                    PushBean pushBean = new PushBean();
                    pushBean.setUrl("https://kedou.aiyuechen.com/api/v2/notice/81");
                    pushBean.setTitle("隐私政策");
                    Intent intent = new Intent(LoginRegisterActivity.this, H5Activity.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("params",pushBean);
                    intent.putExtras(bundle);
                    startActivity(intent);
                }else{
                    ToastUtils.show("请检查您的网络设置");
                }
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                ds.setLinearText(false);
            }
        }, 10, 16, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        //设置文字的前景色
        spannable.setSpan(getRedSpan(),10,16, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv_notice.setText(spannable);
    }

    private ForegroundColorSpan getRedSpan(){
        return new ForegroundColorSpan(ContextCompat.getColor(this,R.color.colorMain));
    }


    //初始化监听器
    private void initListeners(){
        view_back_icon.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                LoginRegisterActivity.this.finish();
            }
        });

        //选择
        llSelect.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if (flag == 0) {
                    ivSelect.setImageResource(R.mipmap.yixuan);
                    flag = 1;
                } else {
                    ivSelect.setImageResource(R.mipmap.weixuan);
                    flag = 0;
                }
            }
        });

        //发送验证码
        tvSendCode.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if(Mutils.isNetworkAvailable()){
                    if(etPhone.getText().toString().trim().equals("")){
                        ToastUtils.show("请输入手机号");
                    }else{
                        sendCodePresenter.sendCode(etPhone.getText().toString().trim());
                        time.start();
                    }
                }else{
                    ToastUtils.show("请检查您的网络设置");
                }
            }
        });

        //注册
        tvLogin.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if(Mutils.isNetworkAvailable()){
                    if(flag==0){
                        ToastUtils.show("请先阅读并同意蝌蚪音基《隐私协议》");
                    } else if(etPhone.getText().toString().trim().equals("")){
                        ToastUtils.show("请输入手机号");
                    }else if(etCode.getText().toString().trim().equals("")){
                        ToastUtils.show("请输入验证码");
                    }else if(etPassword.getText().toString().trim().equals("")){
                        ToastUtils.show("请输入密码");
                    }else if(etRPassword.getText().toString().trim().equals("")){
                        ToastUtils.show("请再次输入密码");
                    }else if(!etPassword.getText().toString().trim().equals(etRPassword.getText().toString().trim())){
                        ToastUtils.show("两次密码输入不一致");
                    }else if(!isSendCode){
                        ToastUtils.show("请先发送验证码");
                    } else{
                        registerPresenter.register(etPhone.getText().toString().trim(),etPassword.getText().toString().trim(),etCode.getText().toString().trim(),String.valueOf(role));
                    }
                }else{
                    ToastUtils.show("请检查您的网络设置");
                }
            }
        });
    }

    @Override
    public void showCode() {
        isSendCode = true;
    }

    @Override
    public void showResult() {
        ToastUtils.show("注册成功");
        this.finish();
    }

    @Override
    public void show401() {
        Intent intent = new Intent(LoginRegisterActivity.this, LoginActivity.class);
        startActivity(intent);
        SpUtil.getInstance(this).putString(Constant.TOKEN, "");
    }


    //倒计时
    class TimeCount extends CountDownTimer {
        TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            tvSendCode.setText(millisUntilFinished / 1000 + "s后重新获取");
            tvSendCode.setClickable(false);
        }

        @Override
        public void onFinish() {
            tvSendCode.setText("获取验证码");
            tvSendCode.setClickable(true);
        }
    }
}
